import 'package:flutter/material.dart';

import '../common/const/app_color_const.dart';
import '../common/const/app_style_const.dart';

@immutable
class CardContainer extends StatefulWidget {
  String? title;
  WidgetBuilder? titleBuilder;
  TextStyle? titleStyle;
  Color? titleRightVerticalLine;
  bool titleRightVerticalLineVisible;
  EdgeInsetsGeometry? margin;
  EdgeInsetsGeometry? titlePadding;
  EdgeInsetsGeometry? bodyPadding;
  EdgeInsetsGeometry? footerPadding;
  WidgetBuilder? titleRightBuilder;
  WidgetBuilder? bodyBuilder;
  WidgetBuilder? footerBuilder;
  Decoration? decoration;
  bool decorationVisible;

  CardContainer(
      {Key? key,
      this.title,
      this.titlePadding,
      this.bodyPadding,
      this.footerPadding,
      this.margin,
      this.titleStyle,
      this.titleBuilder,
      this.titleRightBuilder,
      this.titleRightVerticalLineVisible = false,
      this.bodyBuilder,
      this.footerBuilder,
      this.decorationVisible = true,
      this.decoration})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => CardContainerState();
}

class CardContainerState extends State<CardContainer> {
  Widget buildTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            if (widget.titleRightVerticalLineVisible)
              Container(
                width: 3,
                color: widget.titleRightVerticalLine ?? blackColor,
                child: const Text(''),
              ),
            if (null != widget.title)
              Padding(
                padding: EdgeInsets.only(
                    left: widget.titleRightVerticalLineVisible ? 4.0 : 0.0),
                child: Text(
                  widget.title!,
                  style: widget.titleStyle ?? cardContainerTitleStyle,
                ),
              ),
          ],
        ),
        if (null != widget.titleRightBuilder) widget.titleRightBuilder!(context)
      ],
    );
  }

  Widget buildContent() {
    return Column(
      children: [
        if (null != widget.title || null != widget.titleBuilder)
          Padding(
            padding: widget.titlePadding ??
                const EdgeInsets.only(left: 12, right: 12, top: 12),
            child: (null != widget.titleBuilder)
                ? widget.titleBuilder!(context)
                : buildTitle(),
          ),
        if (null != widget.bodyBuilder)
          Padding(
            padding: widget.bodyPadding ??
                const EdgeInsets.only(left: 12, right: 12, bottom: 12, top: 12),
            child: widget.bodyBuilder!(context),
          ),
        if (null != widget.footerBuilder)
          Padding(
            padding: widget.footerPadding ??
                const EdgeInsets.only(left: 12, right: 12, bottom: 12),
            child: widget.footerBuilder!(context),
          ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: widget.margin ?? const EdgeInsets.all(0.0),
      child: widget.decorationVisible
          ? Container(
              decoration: widget.decoration ??
                  BoxDecoration(
                    color: whiteColor,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 0,
                        spreadRadius: 0,
                        color: shadowColor1.withOpacity(0.0), //阴影颜色
                      ),
                    ],
                    borderRadius: BorderRadius.circular(8),
                  ),
              child: buildContent(),
            )
          : buildContent(),
    );
  }
}
